package com.ja.openchat.ui.contact;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;

import com.google.gson.JsonObject;
import com.ja.openchat.AppViewModelFactory;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.ActivityContactChatMediaBinding;
import com.ja.openchat.databinding.ActivityContactChatSettingsBinding;
import com.ja.openchat.ui.chat.ForwardActivity;
import com.ja.openchat.ui.chat.PreviewWebViewActivity;
import com.ja.openchat.ui.chat.SettingBackgroudActivity;
import com.ja.openchat.ui.message.adapter.SimpleFragmentPagerAdapter;
import com.ja.openchat.viewmodels.ContactChatSettingViewModel;
import com.ja.openchat.viewmodels.ContactDetailSingleViewModel;
import com.zy.openchat.Constants;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResponse;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.message.MessageContent;
import com.zy.openchat.core.model.message.MessageInfo;
import com.zy.openchat.core.model.user.MobileContactInfo;
import com.zy.openchat.core.model.user.UserFullInfo;
import com.zy.openchat.core.model.user.UserInfo;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import butterknife.OnClick;
import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class ContactDetailMediaActivity extends BaseActivity<ActivityContactChatMediaBinding, ContactDetailSingleViewModel> {

    private ChatInfo mChatInfo;
    private UserFullInfo userFullInfo;

    private long chatId = 0;
    public UserInfo user;



    public static void start(Activity activity, UserInfo user, long chatId) {
        Intent intent = new Intent();
        intent.setClass(activity, ContactDetailMediaActivity.class);
        intent.putExtra("user", user);
        intent.putExtra("chatId", chatId);
        activity.startActivity(intent);
    }

    @Override
    public ContactDetailSingleViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(ContactDetailSingleViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_contact_chat_media;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initView() {
        super.initView();
        setTitle(getString(R.string.chat_record));
        binding.toolbar.ivBarBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (getIntent() != null) {
            user = getIntent().getParcelableExtra("user");
            chatId = getIntent().getLongExtra("chatId", 0);
        }
        requestChatInfo(new OnContactChatInfoListener() {
            @Override
            public void onResult(ChatInfo chatInfo) {
                mChatInfo = chatInfo;
                initFragments(mChatInfo);
            }
        });

    }

    private void initFragments(ChatInfo chatInfo){
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(ContactDetailChildFragment.newInstance(0,chatInfo));
        fragments.add(ContactDetailChildFragment.newInstance(1,chatInfo));
        fragments.add(ContactDetailChildFragment.newInstance(2,chatInfo));
        fragments.add(ContactDetailChildFragment.newInstance(3,chatInfo));
        fragments.add(ContactDetailChildFragment.newInstance(4,chatInfo));
        fragments.add(ContactDetailChildFragment.newInstance(5,chatInfo));
        SimpleFragmentPagerAdapter fragmentAdater = new  SimpleFragmentPagerAdapter(getSupportFragmentManager(),fragments,new String[]{
                getString(R.string.media_text),
                getString(R.string.file_text),getString(R.string.voice_text),getString(R.string.link_text),"GIF",getString(R.string.group_text)});
        binding.viewPager.setAdapter(fragmentAdater);
        binding.viewPager.setOffscreenPageLimit(fragments.size());
        binding.tablayout.setupWithViewPager(binding.viewPager);
    }

    private void requestChatInfo(OnContactChatInfoListener listener){
        ChatInfo chatInfo = null;
        if(chatId>0){
            chatInfo = TelegramManager.getTelegramManager().getTgData().chatInfo(chatId);
        }
        if(chatInfo!=null){
            listener.onResult(chatInfo);
        }else if(user!=null){
            TelegramManager.getTelegramManager().createPrivateChat(this, user.id, new TgResultBlock<ChatInfo>() {
                @Override
                public void onResult(Map<String, Object> request, JsonObject response, ChatInfo obj) {
                    if (obj != null) {
                        listener.onResult(obj);
                    }
                }

                @Override
                public void onTimeout(Map<String, Object> request) {
                }
            });
        }else{
            listener.onResult(chatInfo);
        }
    }

}